package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataHandleDto;
import com.zzyl.nursing.dto.AlertDataPageQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-28
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */


    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public int handleAlertData(AlertDataHandleDto alertDataHandleDto) {

        int rows = alertDataMapper.handleAlertData(alertDataHandleDto);
        return rows;
    }

    @Override
    public TableDataInfo selectAlertDataList(AlertDataPageQueryDto alertDataPageQueryDto) {
        //分页查询数据
        Page page = new Page(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryWrapper.eq(AlertData::getUserId, loginUser.getUser().getUserId());
        if (ObjectUtil.isNotEmpty(alertDataPageQueryDto.getDeviceName())) {
            queryWrapper.like(AlertData::getDeviceName, alertDataPageQueryDto.getDeviceName());
        }
        if (ObjectUtil.isNotEmpty(alertDataPageQueryDto.getStatus())) {
            queryWrapper.eq(AlertData::getStatus, alertDataPageQueryDto.getStatus());
        }
        if (ObjectUtil.isNotEmpty(alertDataPageQueryDto.getId())) {
            queryWrapper.eq(AlertData::getId, alertDataPageQueryDto.getId());
        }
        if (ObjectUtil.isNotEmpty(alertDataPageQueryDto.getEndTime()) && ObjectUtil.isNotEmpty(alertDataPageQueryDto.getStartTime())) {
            queryWrapper.between(AlertData::getCreateTime, alertDataPageQueryDto.getStartTime(), alertDataPageQueryDto.getEndTime());
        }
        page = page(page, queryWrapper);
        return getTableDataInfo(page);
    }

    private TableDataInfo getTableDataInfo(Page page) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("请求成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

}
